package com.manor.common.dialog

import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView
import com.lxj.xpopup.interfaces.OnSelectListener
import com.lxj.xpopup.util.XPopupUtils
import com.manor.common.R
import com.manor.common.utils.RecyclerDivider
import kotlinx.android.synthetic.main.dialog_bottom_sheet_check.view.*

/**
 * 打开百度地图
 * @Project TourWZ

 * @date  2019/11/9  16:53
 * @version 1.0
 */
class OpenMapAppDialog (context: Context, val data: MutableList<String>, val listener: OnSelectListener) : BottomPopupView
(context) {
    private lateinit var mAdapter: MyAdapter

    companion object{
        val PACKAGE_NAME_BAIDU="com.baidu.BaiduMap"
        val PACKAGE_NAME_GAODE="com.autonavi.minimap"
        val PACKAGE_NAME_TENGXUN="com.tencent.map"

        fun load(context: Context, listener: OnSelectListener, data: MutableList<String>){
            XPopup.Builder(context)
                    .dismissOnTouchOutside(true)
                    .dismissOnBackPressed(false)
                    .asCustom(OpenMapAppDialog(context, data, listener))
                    .show()
        }
    }


    override fun getImplLayoutId(): Int {
        return R.layout.dialog_bottom_sheet_check
    }

    override fun onShow() {
        super.onShow()
    }

    override fun onDismiss() {
        super.onDismiss()
    }


    override fun onCreate() {
        super.onCreate()
        tvCancel.setOnClickListener { dismiss() }
        mAdapter= MyAdapter(
                R.layout.item_dialog_bottom_sheet_check,
                data
        )
        dialogRecycler.layoutManager= androidx.recyclerview.widget.LinearLayoutManager(context, androidx.recyclerview.widget.RecyclerView.VERTICAL, false)
        dialogRecycler.addItemDecoration(RecyclerDivider(context, LinearLayoutManager.VERTICAL))
        dialogRecycler.adapter = mAdapter
        mAdapter.setOnItemClickListener {
            _, _, position ->
            dismiss()
            listener.onSelect(position, data[position])
        }

    }


    // 最大高度为Window的0.85
    override fun getMaxHeight(): Int {
        return (XPopupUtils.getWindowHeight(context) * .75f).toInt()
    }

    private class MyAdapter(layout: Int, data: MutableList<String>)
        : BaseQuickAdapter<String, BaseViewHolder>(layout, data) {
        override fun convert(holder: BaseViewHolder, item: String) {
            holder.setText(R.id.tvText, item)
        }
    }


}